package hrms.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import hrms.dao.AdminDao;
import hrms.dao.TrainDao;
import hrms.entity.Train;
import hrms.util.PageUtil;
import hrms.util.Result;

@Service("trainService")
public class TrainServiceImpl implements TrainService{
	
	@Resource(name="adminDao")
	private AdminDao adminDao ;
	
	@Resource(name="trainDao")
	private TrainDao dao;

	
	public Result<List<Train>> findByPage(String pages ,HttpServletResponse res) {
		String message = "Train";
		return new PageUtil<Train>().findByPage(pages, res, dao, message);
	}
	
	
	public Result<Map<String, Integer>> pageTotal(HttpServletRequest req){
		String message = "Train";
		return PageUtil.pageTotal(req, message); 
	}
	
	
	public Result<Object> addTrain(String tcourse,String   tcher, String tstu, String tplace,String tdate,String ttime) {
		Result<Object> result = new Result<Object>();
		
		Train e = new Train();
		e.settCourse(tcourse);
		e.settCher(tcher);
		e.settDate(tdate);
		e.settPlace(tplace);
		e.settStu(tstu);
		e.settTime(ttime);
		int num = dao.saveTrain(e);
		if(num!=1){
			result.setStatus(1);
			result.setMsg("创建出错！");
		} else {
			result.setStatus(0);
			result.setMsg("创建成功！");
		}
		return result;
	}


	public Result<List<Train>> search(String name) {
		Result<List<Train>> result = new Result<List<Train>>();
	    List<Train> list = dao.findSearch("%"+name+"%");
		result.setData(list);
		result.setStatus(0);
		return result;
	}

	/*
	public Result<Object> changeState(String TrainId, HttpServletRequest req) {
		Result<Object> result = new Result<Object>();
		int num = dao.changeState(TrainId);
		
		Cookie[] c = req.getCookies();
		String page = "1";
		for(Cookie cc : c){
			if(cc.getName().equals("TrainPage")){
				page = cc.getValue();
			}
		}
		int pageNum = Integer.parseInt(page);
		if(num != 1){
			result.setStatus(1);
			result.setMsg("修改出错");
		} else {
			result.setStatus(0);
			result.setMsg("修改成功");
			result.setData(pageNum);
		}
		return result;
	}

	/*
	public Result<Train> findTrain(String TrainId, HttpServletRequest req) {
		Train e = dao.findByTrainId(Integer.parseInt(TrainId));
		Result<Train> r = new Result<Train>();
		if(e != null){
			r.setData(e);
			r.setMsg("");
			r.setStatus(0);
			return r;
		}
		else{
			r.setStatus(1);
			r.setMsg("未找到员工信息！");
			return null;
		}
		
		
	}

	public Result<Object> uppdateTrain(String TrainId, String TrainName, String TrainSex, String TrainAge, String TrainJob,
			String TrainDept,HttpServletRequest req) {
			Result<Object> result = new Result<Object>();
			//获取当前登录的用户
			Admin adminUser = (Admin)req.getSession().getAttribute("admin");
			Integer rollUser = adminUser.getRoll();
			//获取将要被删除的部门号
			
			if(rollUser == 2 ){
				result.setStatus(2);
				result.setMsg("权限不够!");
				return result;
			}
			dao.deleteTrain(Integer.parseInt(TrainId));
			Train e = new Train();
			e.setTrainId(Integer.parseInt(TrainId));
			e.setAge(Integer.parseInt(TrainAge));
			e.setTrainName(TrainName);
			e.setGender(TrainSex);
			e.setHiredate(new Date(System.currentTimeMillis()));
			String deptId = dao.findDeptIdByName(TrainDept);
			if(deptId == null){
				result.setStatus(3);
				result.setMsg("部门不存在！");
				return result;
			}
			e.setdName(TrainDept);
			e.setDeptId(Integer.parseInt(deptId));
			e.setJob(TrainJob);
			
			int num = dao.updateTrain(e);
			if(num!=1){
				result.setStatus(1);
				result.setMsg("修改出错！");
			} else {
				result.setStatus(0);
				result.setMsg("修改成功！");
			}
		return result;
	}
	*/
}